<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="params.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" />
        <!-- 树形组件 -->
        <el-tree ref="treeDom" node-key="id" :expand-on-click-node="false" :data="treeData" :props="defaultProps" default-expand-all :highlight-current="true" @current-change="selectTreeId" />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="isShow=true">excel导入</el-button>
          <el-button size="mini" @click="downloadExcel">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table
          :data="tableData"
          style="width: 100%"
        >
          <el-table-column
            fixed
            prop="staffPhoto"
            label="头像"
          >
            <template v-slot="{row}">
              <el-avatar v-if="row.staffPhoto" size="small" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username[0] }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="username"
            label="姓名"
          />
          <el-table-column
            prop="mobile"
            label="手机号"
            sortable
          />
          <el-table-column
            prop="workNumber"
            label="工号"
            sortable
          />
          <el-table-column
            prop="formOfEmployment"
            label="聘用形式"
          >
            <template v-slot="{row}">
              <span v-if="row.formOfEmployment===1">正式</span>
              <span v-else-if="row.formOfEmployment===2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="departmentName"
            label="部门"
          />
          <el-table-column
            prop="timeOfEntry"
            label="入职时间"
            sortable
          />
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="280"
          >
            <template v-slot="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small" >角色</el-button>
              <el-popconfirm
                title="确定要删除吗？"
                @onConfirm="delEmployeeFn(row.id)"
              ><el-button slot="reference" size="small" style="margin-left:10px;" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="end" style="height: 60px;margin-top: 20px;">
          <el-pagination
            :current-page.sync="params.page"
            :page-size="params.pagesize"
            layout="total, prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </div>
    </div>
    <ImportExcel :show-excel-dialog.sync="isShow" @getEmployeeList="getEmployeeList" />
  </div>
</template>

<script>
import ImportExcel from './components/import-excel'
import FileSave from 'file-saver'
import _ from 'lodash'
import { downloadExcelApi, getEmployeeApi, delEmployeeApi } from '@/api/employee'
import { getDepartListApi } from '@/api/department'
import { transList2Tree } from '@/utils'
export default {
  name: 'Employee',
  components: { ImportExcel },
  data() {
    return {
      // employeeRole: ['系统管理员', '人事经理', '人事专员', '薪资专员', '员工', '总经理', '软件设计', '软件开发', '喵喵'],
      // isDiglog: false,
      isShow: false,
      total: 0,
      tableData: [],
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      params: {
        page: 1,
        pagesize: 10,
        keyword: '',
        departmentId: ''
      }
    }
  },
  created() {
    this.getTreeData()
  },
  watch: {
    'params.keyword': _.debounce(function() {
      this.params.page = 1
      this.getEmployeeList()
    })
  },
  methods: {
    async getTreeData() {
      const res = await getDepartListApi()
      this.treeData = transList2Tree(res, 0)
      this.params.departmentId = this.treeData[0].id// 获取传智教育id
      // 数据渲染完毕后就默认选中第一个节点
      this.$nextTick(() => {
        this.$refs.treeDom.setCurrentKey(this.params.departmentId)
      })
      this.getEmployeeList()
    },
    async getEmployeeList() {
      const res = await getEmployeeApi(this.params)
      // console.log(res)
      this.tableData = res.rows
      this.total = res.total
    },
    handleCurrentChange(page) {
      // 将点击的页码赋值
      this.params.page = page
      // 获取最新的页码数据
      this.getEmployeeList()
    },
    selectTreeId(data) {
      this.params.departmentId = data.id
      this.params.page = 1
      this.getEmployeeList()
    },
    async downloadExcel() {
      const Blob = await downloadExcelApi()
      const fileName = Math.floor(Math.random() * 10)
      FileSave.saveAs(Blob, `员工信息表-${fileName}.xlsx`)
    },
    async delEmployeeFn(id) {
      await delEmployeeApi(id)
      this.$message.success('删除员工成功')
      // if (this.tableData.length === 1 && this.params.page > 1) {
      //   this.params.page--
      // }
      this.getEmployeeList()
    }

  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
